package com.prs.crm.action.archives;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.archives.ArchivesType;
import com.prs.crm.domain.oa.Archives;
import com.prs.crm.service.archives.ArchivesTypeService;

public class ArchivesTypeAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private ArchivesType archivesType;
	private ArchivesTypeService archivesTypeService;
	private PaginationSupport pagination;
	private List<ArchivesType> archives;
	private TreeNode root;
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public List<ArchivesType> getArchives() {
		return archives;
	}
	public void setArchives(List<ArchivesType> archives) {
		this.archives = archives;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public ArchivesType getArchivesType() {
		return archivesType;
	}
	public void setArchivesType(ArchivesType archivesType) {
		this.archivesType = archivesType;
	}
	public ArchivesTypeService getArchivesTypeService() {
		return archivesTypeService;
	}
	public void setArchivesTypeService(ArchivesTypeService archivesTypeService) {
		this.archivesTypeService = archivesTypeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	
	@Action("listArchivesType")
	public String listArchivesType(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ArchivesType.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getArchivesTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editArchivesType")
	public String editArchivesType(){
		if(ids!=null){
			this.archivesType=this.getArchivesTypeService().get(ids[0]);
		}
		this.archives=this.getArchivesTypeService().getAllArchivesType();
		return SUCCESS;
	}
	
	@Action(value="saveArchivesType",results={
			@Result(name="success",type="redirect",location="listArchivesType"),
			@Result(name="error",type="chain",location="editArchivesType")
	})
	public String saveArchivesType(){
		if (archivesType.getNumber()==null || "".equals(archivesType.getNumber().trim())) {
			addActionError("类目编号不能为空！");
			return ERROR;
		}
		if (archivesType.getName()==null || "".equals(archivesType.getName().trim())) {
			addActionError("类目名称不能为空");
			return ERROR;
		}
		if (archivesType.getId()==null) {
			List<ArchivesType> at=this.getArchivesTypeService().getAllArchivesType();
			int a=0;
			int b=0;
			for (ArchivesType ar : at) {
				if (ar.getName().equals(archivesType.getName())) {
					a++;
				}
				if (ar.getNumber().equals(archivesType.getNumber())) {
					b++;
				}
			}
			if (a>0) {
				this.addActionError("类目名称重复！");
				return ERROR;
			}
			if (b>0) {
				this.addActionError("类目编号重复！");
				return ERROR;
			}	
			this.getArchivesTypeService().save(archivesType);
		}else{
			this.getArchivesTypeService().submit(archivesType);
		}
		return SUCCESS;
	}
	
	
	@Action(value="removeArchivesType",results={
			@Result(name="success",type="redirect",location="listArchivesType"),
			@Result(name="error",type="chain",location="listArchivesType")
	})
	public String removeArchivesType(){
		if(ids!=null){
			this.archivesType=this.getArchivesTypeService().get(ids[0]);
		}
		if (archivesType.getChilds().size()!=0) {
			this.addActionError("已有下级目录不能删除！");
			return ERROR;
		}
		if (archivesType.getArchivesManager().size()!=0) {
			this.addActionError("该类目下已有档案不能删除！");
			return ERROR;
		}
		this.getArchivesTypeService().batchRemove(ids);
		return SUCCESS;
	}
	
	
	@Action(value = "loadArchivesTypeTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadArchivesTypeTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("档案类目");

		Collection<ArchivesType> Archivess = this.getArchivesTypeService().getRootArchivesType();

		for (ArchivesType Archives : Archivess) {
			TreeNode node = new TreeNode();
			node.setText(Archives.getName()+"["+Archives.getNumber()+"]");
			node.setId(Archives.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, Archives);
		}

		root.addChildNode(firstNode);
		return SUCCESS;
	}
	private void addDepartNode(TreeNode parent, ArchivesType dept) {
		for (ArchivesType depart : dept.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(depart.getName()+"["+depart.getNumber()+"]");
			node.setId(depart.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, depart);
		}
	}
}
